package com.xuexiang.xqrcode.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.text.TextUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.encoder.ByteMatrix;
import com.google.zxing.qrcode.encoder.Encoder;
import com.google.zxing.qrcode.encoder.QRCode;
import com.xuexiang.xqrcode.logs.QCLog;
import java.util.HashMap;
import java.util.Hashtable;

/* loaded from: classes2.dex */
public final class QRCodeProduceUtils {
    private static final int BYTE_AGN = 3;
    private static final int BYTE_DTA = 1;
    private static final int BYTE_EPT = 0;
    private static final int BYTE_POS = 2;
    private static final int BYTE_PTC = 5;
    private static final int BYTE_TMG = 4;
    private static int DEFAULT_BINARIZING_THRESHOLD = 128;
    private static float DEFAULT_DATA_DOT_SCALE = 0.3f;
    private static int DEFAULT_MARGIN = 20;
    public static final int QRCODE_BITMAP_MAX_SIZE = 400;

    private static boolean isTypePOS(int i, int i2, int i3, boolean z) {
        if (z) {
            if (i >= 7 || (i2 >= 7 && i2 < i3 - 7)) {
                return i >= i3 - 7 && i2 < 7;
            }
            return true;
        } else if (i > 7 || (i2 > 7 && i2 < i3 - 8)) {
            return i >= i3 + (-8) && i2 <= 7;
        } else {
            return true;
        }
    }

    private static boolean isTypeTMG(int i, int i2, int i3) {
        return (i2 == 6 && i >= 8 && i < i3 + (-8)) || (i == 6 && i2 >= 8 && i2 < i3 - 8);
    }

    private QRCodeProduceUtils() {
        throw new UnsupportedOperationException("Do not need instantiate!");
    }

    public static Builder newBuilder(String str) {
        return new Builder(str);
    }

    /* loaded from: classes2.dex */
    public static class Builder {
        Bitmap backgroundImage;
        String contents;
        int size = 400;
        int margin = QRCodeProduceUtils.DEFAULT_MARGIN;
        float dataDotScale = QRCodeProduceUtils.DEFAULT_DATA_DOT_SCALE;
        boolean autoColor = true;
        int colorDark = -16777216;
        int colorLight = -1;
        boolean whiteMargin = false;
        boolean binarize = false;
        int binarizeThreshold = QRCodeProduceUtils.DEFAULT_BINARIZING_THRESHOLD;

        public Builder(String str) {
            this.contents = str;
        }

        public Builder setContents(String str) {
            this.contents = str;
            return this;
        }

        public Builder setSize(int i) {
            this.size = i;
            return this;
        }

        public Builder setMargin(int i) {
            this.margin = i;
            return this;
        }

        public Builder setDataDotScale(float f) {
            this.dataDotScale = f;
            return this;
        }

        public Builder setColorDark(int i) {
            this.colorDark = i;
            return this;
        }

        public Builder setColorLight(int i) {
            this.colorLight = i;
            return this;
        }

        public Builder setBackgroundImage(Bitmap bitmap) {
            this.backgroundImage = bitmap;
            return this;
        }

        public Builder setWhiteMargin(boolean z) {
            this.whiteMargin = z;
            return this;
        }

        public Builder setAutoColor(boolean z) {
            this.autoColor = z;
            return this;
        }

        public Builder setBinarize(boolean z) {
            this.binarize = z;
            return this;
        }

        public Builder setBinarizeThreshold(int i) {
            this.binarizeThreshold = i;
            return this;
        }

        public Bitmap build() {
            return QRCodeProduceUtils.create(this.contents, this.size, this.margin, this.dataDotScale, this.colorDark, this.colorLight, this.backgroundImage, this.whiteMargin, this.autoColor, this.binarize, this.binarizeThreshold);
        }
    }

    public static Bitmap create(String str, int i, int i2, int i3, int i4) throws IllegalArgumentException {
        return create(str, i, i2, DEFAULT_DATA_DOT_SCALE, i3, i4, null, true, true);
    }

    public static Bitmap create(String str, int i, int i2, int i3, int i4, Bitmap bitmap) throws IllegalArgumentException {
        return create(str, i, i2, DEFAULT_DATA_DOT_SCALE, i3, i4, bitmap, true, true);
    }

    public static Bitmap create(String str, int i, int i2, int i3, int i4, Bitmap bitmap, boolean z) throws IllegalArgumentException {
        return create(str, i, i2, DEFAULT_DATA_DOT_SCALE, i3, i4, bitmap, z, true);
    }

    public static Bitmap create(String str, int i, int i2, float f, int i3, int i4, Bitmap bitmap, boolean z) throws IllegalArgumentException {
        return create(str, i, i2, f, i3, i4, bitmap, z, false);
    }

    public static Bitmap create(String str, int i, int i2, float f, Bitmap bitmap, boolean z, boolean z2) throws IllegalArgumentException {
        return create(str, i, i2, f, -16777216, -1, bitmap, z, true, z2, DEFAULT_BINARIZING_THRESHOLD);
    }

    public static Bitmap create(String str, int i, int i2, float f, Bitmap bitmap, boolean z, boolean z2, int i3) throws IllegalArgumentException {
        return create(str, i, i2, f, -16777216, -1, bitmap, z, true, z2, i3);
    }

    public static Bitmap create(String str, int i, int i2, float f, int i3, int i4, Bitmap bitmap, boolean z, boolean z2) throws IllegalArgumentException {
        return create(str, i, i2, f, i3, i4, bitmap, z, z2, false, DEFAULT_BINARIZING_THRESHOLD);
    }

    public static Bitmap create(String str, int i, int i2, float f, int i3, int i4, Bitmap bitmap, boolean z, boolean z2, boolean z3) throws IllegalArgumentException {
        return create(str, i, i2, f, i3, i4, bitmap, z, z2, z3, DEFAULT_BINARIZING_THRESHOLD);
    }

    public static Bitmap create(String str, int i, int i2, float f, int i3, int i4, Bitmap bitmap, boolean z, boolean z2, boolean z3, int i5) throws IllegalArgumentException {
        if (str.isEmpty()) {
            throw new IllegalArgumentException("Error: contents is empty. (contents.isEmpty())");
        }
        if (i >= 0) {
            if (i2 >= 0) {
                int i6 = i - (i2 * 2);
                if (i6 <= 0) {
                    throw new IllegalArgumentException("Error: there is no space left for the QRCode. (size - 2 * margin <= 0)");
                }
                ByteMatrix bitMatrix = getBitMatrix(str);
                if (i6 >= bitMatrix.getWidth()) {
                    if (f < 0.0f || f > 1.0f) {
                        throw new IllegalArgumentException("Error: an illegal data dot scale is given. (dataDotScale < 0 || dataDotScale > 1)");
                    }
                    return render(bitMatrix, i6, i2, f, i3, i4, bitmap, z, z2, z3, i5);
                }
                throw new IllegalArgumentException("Error: there is no space left for the QRCode. (size - 2 * margin < " + bitMatrix.getWidth() + ")");
            }
            throw new IllegalArgumentException("Error: a negative margin is given. (margin < 0)");
        }
        throw new IllegalArgumentException("Error: a negative size is given. (size < 0)");
    }

    private static Bitmap render(ByteMatrix byteMatrix, int i, int i2, float f, int i3, int i4, Bitmap bitmap, boolean z, boolean z2, boolean z3, int i5) {
        int i6;
        String str;
        float width = i / byteMatrix.getWidth();
        Bitmap createBitmap = Bitmap.createBitmap((z ? 0 : i2 * 2) + i, (z ? 0 : i2 * 2) + i, Bitmap.Config.ARGB_8888);
        if (bitmap != null) {
            scaleBitmap(bitmap, createBitmap);
        }
        int i7 = i + (i2 * 2);
        Bitmap createBitmap2 = Bitmap.createBitmap(i7, i7, Bitmap.Config.ARGB_8888);
        int dominantColor = (!z2 || bitmap == null) ? i3 : getDominantColor(bitmap);
        if (!z3 || bitmap == null) {
            i6 = i4;
        } else {
            int i8 = DEFAULT_BINARIZING_THRESHOLD;
            if (i5 > 0 && i5 < 255) {
                i8 = i5;
            }
            dominantColor = -16777216;
            binarize(createBitmap, i8);
            i6 = -1;
        }
        Paint paint = new Paint();
        Paint paint2 = new Paint();
        paint2.setColor(dominantColor);
        byte b = 1;
        paint2.setAntiAlias(true);
        Paint paint3 = new Paint();
        paint3.setColor(i6);
        paint3.setAntiAlias(true);
        Paint paint4 = new Paint();
        paint4.setColor(Color.argb(120, 255, 255, 255));
        paint4.setAntiAlias(true);
        Canvas canvas = new Canvas(createBitmap2);
        canvas.drawColor(-1);
        canvas.drawBitmap(createBitmap, z ? i2 : 0.0f, z ? i2 : 0.0f, paint);
        int i9 = 0;
        while (i9 < byteMatrix.getHeight()) {
            String str2 = "";
            int i10 = 0;
            while (i10 < byteMatrix.getWidth()) {
                byte b2 = byteMatrix.get(i10, i9);
                if (b2 == 0) {
                    float f2 = i2;
                    float f3 = i10;
                    float f4 = (1.0f - f) * 0.5f;
                    float f5 = i9;
                    float f6 = (f + 1.0f) * 0.5f;
                    canvas.drawRect(f2 + ((f3 + f4) * width), ((f4 + f5) * width) + f2, ((f3 + f6) * width) + f2, f2 + ((f5 + f6) * width), paint3);
                    str = str2 + "\u3000";
                } else if (b2 == b) {
                    float f7 = i2;
                    float f8 = i10;
                    float f9 = (1.0f - f) * 0.5f;
                    float f10 = i9;
                    float f11 = (f + 1.0f) * 0.5f;
                    canvas.drawRect(f7 + ((f8 + f9) * width), ((f9 + f10) * width) + f7, ((f8 + f11) * width) + f7, f7 + ((f10 + f11) * width), paint2);
                    str = str2 + "〇";
                } else if (b2 == 2 || b2 == 3 || b2 == 4) {
                    float f12 = i2;
                    float f13 = i10;
                    float f14 = i9;
                    canvas.drawRect((f13 * width) + f12, f12 + (f14 * width), ((f13 + 1.0f) * width) + f12, f12 + ((f14 + 1.0f) * width), paint2);
                    str = str2 + "Ｘ";
                } else if (b2 != 5) {
                    i10++;
                    b = 1;
                } else {
                    float f15 = i2;
                    float f16 = i10;
                    float f17 = i9;
                    canvas.drawRect(f15 + (f16 * width), f15 + (f17 * width), ((f16 + 1.0f) * width) + f15, ((f17 + 1.0f) * width) + f15, paint4);
                    str = str2 + "＋";
                }
                str2 = str;
                i10++;
                b = 1;
            }
            QCLog.dTag("QR_MAPPING", str2);
            i9++;
            b = 1;
        }
        return createBitmap2;
    }

    private static ByteMatrix getBitMatrix(String str) {
        try {
            QRCode protoQRCode = getProtoQRCode(str, ErrorCorrectionLevel.H);
            int[] alignmentPatternCenters = protoQRCode.getVersion().getAlignmentPatternCenters();
            ByteMatrix matrix = protoQRCode.getMatrix();
            int width = matrix.getWidth();
            for (int i = 0; i < width; i++) {
                for (int i2 = 0; i2 < width; i2++) {
                    if (isTypeAGN(i2, i, alignmentPatternCenters, true)) {
                        if (matrix.get(i2, i) != 0) {
                            matrix.set(i2, i, 3);
                        } else {
                            matrix.set(i2, i, 5);
                        }
                    } else if (isTypePOS(i2, i, width, true)) {
                        if (matrix.get(i2, i) != 0) {
                            matrix.set(i2, i, 2);
                        } else {
                            matrix.set(i2, i, 5);
                        }
                    } else if (isTypeTMG(i2, i, width)) {
                        if (matrix.get(i2, i) != 0) {
                            matrix.set(i2, i, 4);
                        } else {
                            matrix.set(i2, i, 5);
                        }
                    }
                    if (isTypePOS(i2, i, width, false) && matrix.get(i2, i) == 0) {
                        matrix.set(i2, i, 5);
                    }
                }
            }
            return matrix;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static QRCode getProtoQRCode(String str, ErrorCorrectionLevel errorCorrectionLevel) throws WriterException {
        if (str.isEmpty()) {
            throw new IllegalArgumentException("Found empty contents");
        }
        HashMap hashMap = new HashMap();
        hashMap.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
        return Encoder.encode(str, errorCorrectionLevel, hashMap);
    }

    private static boolean isTypeAGN(int i, int i2, int[] iArr, boolean z) {
        if (iArr.length == 0) {
            return false;
        }
        int i3 = iArr[iArr.length - 1];
        for (int i4 : iArr) {
            for (int i5 : iArr) {
                if ((!z || i5 == 6 || i4 == 6 || i5 == i3 || i4 == i3) && (!(i5 == 6 && i4 == 6) && (!(i5 == 6 && i4 == i3) && (!(i4 == 6 && i5 == i3) && i >= i5 - 2 && i <= i5 + 2 && i2 >= i4 - 2 && i2 <= i4 + 2)))) {
                    return true;
                }
            }
        }
        return false;
    }

    private static void scaleBitmap(Bitmap bitmap, Bitmap bitmap2) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setFilterBitmap(true);
        float width = bitmap2.getWidth() / bitmap.getWidth();
        float height = bitmap2.getHeight() / bitmap.getHeight();
        float width2 = bitmap2.getWidth() * 0.5f;
        float height2 = bitmap2.getHeight() * 0.5f;
        Matrix matrix = new Matrix();
        matrix.setScale(width, height, width2, height2);
        Canvas canvas = new Canvas(bitmap2);
        canvas.setMatrix(matrix);
        canvas.drawBitmap(bitmap, width2 - (bitmap.getWidth() / 2), height2 - (bitmap.getHeight() / 2), paint);
    }

    private static int getDominantColor(Bitmap bitmap) {
        Bitmap createScaledBitmap = Bitmap.createScaledBitmap(bitmap, 8, 8, true);
        int i = 0;
        int i2 = 0;
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        while (i < createScaledBitmap.getHeight()) {
            int i6 = i4;
            int i7 = i2;
            for (int i8 = 0; i8 < createScaledBitmap.getHeight(); i8++) {
                int pixel = createScaledBitmap.getPixel(i8, i);
                int i9 = (pixel >> 16) & 255;
                int i10 = (pixel >> 8) & 255;
                int i11 = pixel & 255;
                if (i9 <= 200 && i10 <= 200 && i11 <= 200) {
                    i7 += i9;
                    i6 += i10;
                    i5 += i11;
                    i3++;
                }
            }
            i++;
            i2 = i7;
            i4 = i6;
        }
        createScaledBitmap.recycle();
        return (Math.max(0, Math.min(255, i2 / i3)) << 16) | (-16777216) | (Math.max(0, Math.min(255, i4 / i3)) << 8) | Math.max(0, Math.min(255, i5 / i3));
    }

    private static void binarize(Bitmap bitmap, int i) {
        for (int i2 = 0; i2 < bitmap.getHeight(); i2++) {
            for (int i3 = 0; i3 < bitmap.getHeight(); i3++) {
                int pixel = bitmap.getPixel(i3, i2);
                bitmap.setPixel(i3, i2, ((((float) ((pixel >> 16) & 255)) * 0.3f) + (((float) ((pixel >> 8) & 255)) * 0.59f)) + (((float) (pixel & 255)) * 0.11f) > ((float) i) ? -1 : -16777216);
            }
        }
    }

    public static Bitmap create(String str, int i, int i2, Bitmap bitmap) {
        int i3;
        int i4;
        int i5;
        int i6;
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            Bitmap scaleLogo = getScaleLogo(bitmap, i, i2);
            int i7 = i / 2;
            int i8 = i2 / 2;
            if (scaleLogo != null) {
                int width = scaleLogo.getWidth();
                int height = scaleLogo.getHeight();
                i5 = width;
                i6 = height;
                i3 = (i - width) / 2;
                i4 = (i2 - height) / 2;
            } else {
                i3 = i7;
                i4 = i8;
                i5 = 0;
                i6 = 0;
            }
            Hashtable hashtable = new Hashtable();
            hashtable.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hashtable.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            hashtable.put(EncodeHintType.MARGIN, 0);
            BitMatrix encode = new QRCodeWriter().encode(str, BarcodeFormat.QR_CODE, i, i2, hashtable);
            int[] iArr = new int[i * i2];
            for (int i9 = 0; i9 < i2; i9++) {
                for (int i10 = 0; i10 < i; i10++) {
                    int i11 = -16777216;
                    if (i10 >= i3 && i10 < i3 + i5 && i9 >= i4 && i9 < i4 + i6) {
                        int pixel = scaleLogo.getPixel(i10 - i3, i9 - i4);
                        if (pixel != 0) {
                            i11 = pixel;
                        } else if (!encode.get(i10, i9)) {
                            i11 = -1;
                        }
                        iArr[(i9 * i) + i10] = i11;
                    } else if (encode.get(i10, i9)) {
                        iArr[(i9 * i) + i10] = -16777216;
                    } else {
                        iArr[(i9 * i) + i10] = -1;
                    }
                }
            }
            Bitmap createBitmap = Bitmap.createBitmap(i, i2, Bitmap.Config.ARGB_8888);
            createBitmap.setPixels(iArr, 0, i, 0, 0, i, i2);
            return createBitmap;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static Bitmap getScaleLogo(Bitmap bitmap, int i, int i2) {
        if (bitmap == null) {
            return null;
        }
        Matrix matrix = new Matrix();
        float min = Math.min(((i * 1.0f) / 5.0f) / bitmap.getWidth(), ((i2 * 1.0f) / 5.0f) / bitmap.getHeight());
        matrix.postScale(min, min);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
}
